package com.carrie.mdg.myviewtext.activity;

import android.content.Context;
import android.content.Intent;
import android.icu.text.DateFormat;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.aidl.Book;

public class AndroidThreadTestActivity extends AppCompatActivity {
    public static final int UPDATE_TEXT = 1;

    private Button changeText;
    private TextView text;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATE_TEXT:
                    text.setText("Nice to meet you.");
                    break;
                default:
                    break;
            }
        }
    };

    public static void actionStart(Context context){
        Intent intent = new Intent(context,AndroidThreadTestActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_thread_test);
        changeText = (Button) findViewById(R.id.change_text);
        text = (TextView) findViewById(R.id.text);

        changeText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message message = mHandler.obtainMessage();
                        message.what = UPDATE_TEXT;
                        mHandler.sendMessage(message);
                    }
                }).start();
            }
        });
    }

    class DownloadTask extends AsyncTask<Void,Integer,Boolean>{

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            publishProgress(10);

            publishProgress(20);
            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
        }
    }
}
